package com.example.listener.page

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.*
import com.example.listener.databinding.FragmentWifiBinding
import com.example.listener.databinding.ItemWifiListBinding
import com.seed.launcher.listener.bean.WifiInfo
import com.seed.launcher.listener.wifi.OnWifiChangedListener
import com.seed.launcher.listener.wifi.WifiListener

/**
 *
 * @author: xct
 * create on: 2022/8/30 10:57
 *
 */
class WifiFragment : Fragment() {

    private lateinit var viewBinding: FragmentWifiBinding

    private val mAdapter by lazy { WifiListAdapter() }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return FragmentWifiBinding.inflate(inflater, container, false)
            .also { viewBinding = it }
            .root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val context = view.context
        viewBinding.apply {
            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.adapter = mAdapter
            if (recyclerView.itemAnimator is SimpleItemAnimator) {
                (recyclerView.itemAnimator as SimpleItemAnimator)
                    .apply {
                        changeDuration = 0
                    }
                    .supportsChangeAnimations = false
            }
        }
        val wifiListener = WifiListener(context, object : OnWifiChangedListener {
            override fun onCurrentWifiChanged(wifiInfo: WifiInfo?) {
                wifiInfo ?: return
                viewBinding.tvCurrent.text = wifiInfo.toString()
                Toast.makeText(requireContext(), "current wifi changed.", Toast.LENGTH_SHORT).show()
            }

            override fun onWifiListChanged(wifiInfoList: MutableList<WifiInfo>?) {
                wifiInfoList ?: return
                viewBinding.recyclerView.post {
                    mAdapter.submitList(wifiInfoList)
                }
                viewBinding.tvMessage.text = "${wifiInfoList.size} AP found"
            }
        })
        viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {
                super.onCreate(owner)
                wifiListener.register()
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                wifiListener.unregister()
            }
        })
    }

    inner class WifiItemViewHolder(val viewBinding: ItemWifiListBinding) :
        RecyclerView.ViewHolder(viewBinding.root)

    inner class WifiListAdapter :
        ListAdapter<WifiInfo, WifiItemViewHolder>(object : DiffUtil.ItemCallback<WifiInfo>() {
            override fun areItemsTheSame(oldItem: WifiInfo, newItem: WifiInfo): Boolean {
                return oldItem === newItem
            }

            override fun areContentsTheSame(oldItem: WifiInfo, newItem: WifiInfo): Boolean {
                return oldItem == newItem
            }
        }) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WifiItemViewHolder {
            val context = parent.context
            return WifiItemViewHolder(
                (ItemWifiListBinding.inflate(
                    LayoutInflater.from(context),
                    parent,
                    false
                ))
            )
        }

        override fun onBindViewHolder(holder: WifiItemViewHolder, position: Int) {
            val item: WifiInfo = getItem(position)

            holder.viewBinding.apply {
                tvItem.text = item.toString()
            }

        }
    }
}